<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Role;
use App\Permission;

class RoleController extends Controller
{
    //
    protected function index(){
    	
		$list = Role::get();
    	return view('admin/role/index',['list'=>$list]);
    }
	
	//角色添加
    protected function add(){
		if(request()->isMethod('post')){
			$input = request()->only(['name','display_name','description']);
			$role = new Role();
			$res = $role->check($input);
			if($res){
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> $res,
					'url'	=>''
					));					
			}			
			$role->name 		 = $input['name'];
			if(request()->has('display_name')) $role->display_name = $input['display_name'];
			if(request()->has('description')) $role->description	 = $input['description'];
			
			
			if($role->save()){
				return response()->json(array(
					'code'	=> 1,
					'msg'	=> '添加成功',
					'url'	=> route('admin.role.index')
					));		
			}else{
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> '添加成功',
					'url'	=>''
					));						
			}
				
		}
    	return view('admin/role/add');
    }
	//删除
	protected function del(){
		$id = request('id');
		$role = Role::find($id);
		if($role->delete()){
			return response()->json(array(
				'code'	=> 1,
				'msg'	=> '删除成功',
				'url'	=>''
				));					
		}else{
			return response()->json(array(
				'code'	=> 0,
				'msg'	=> '删除失败',
				'url'	=>''
				));				
		}		
	}	
	
	//编辑
	protected function edit(){
		if(request()->isMethod('post')){
			$input = request()->only(['id','name','display_name','description']);
			$role = Role::find($input['id']);
			$role->name 		 = $input['name'];
			$role->display_name  = $input['display_name'];
			$role->description	 = $input['description'];
			if($role->save()){
				return response()->json(array(
					'code'	=> 1,
					'msg'	=> '编辑成功',
					'url'	=> route('admin.role.index')
					));		
			}else{
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> '编辑成功',
					'url'	=>''
					));						
			}			
		}
		$id = request('id');
		$data = Role::find($id);
		return view('admin/role/edit',['data'=>$data]);
	}
	
	//授权
	protected function auth(){
		$id = request('id');
		if(request()->isMethod('post')){
			$a = Permission::leftjoin('permission_role',function($join)use($id){
				$join->on('permission_role.permission_id','=','permissions.id')->where('permission_role.role_id','=',$id);
			})
			->select('permissions.id','permissions.display_name as title','permissions.pid','permission_role.role_id')
			->get()->toarray();			
			foreach ($a as $k=>$v) {
				if($v['role_id']){
					$a[$k]['checked'] = TRUE;
				}else{
					$a[$k]['checked'] = FALSE;
				}	
			}			
			return response()->json($a);				
		}
		return view('admin/role/auth',['id'=>$id]);
	}
	
	//更新授权
	protected function upauth(){
		$role_id = request('id');
		$ids = request('auth_rule_ids');
		$role = Role::find($role_id);
		if(empty($ids)){
			$role->perms()->sync([]);
		}else{
			$role->perms()->sync($ids);
		}
		return  response()->json([
			'code'	=>1,
			'msg'	=>'更新权限成功！',
			'url'	=>route('admin.role.auth',['id'=>$role_id])
			]);
	}
	
}
